<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;"/>
    <title>安平通行码</title>
    <script src="./jquery/jquery.min.js"></script>
    <script src="../api/api.js"></script>
</head>

<body>
    <script>
        // 小区页面
        function getQueryVariable(variable) {
            var query = decodeURI(window.location.search.substring(1))
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) {
                    return pair[1];
                }
            }
            return (false);
        }
        var code = getQueryVariable('code')
        var rukouName = getQueryVariable('state')
        function getCode(urls) {
            $.ajax({
                type: "get",
                url: "http://user.city1000.top/wechat/wxScan", //自己填写请求地址
                data: {
                    url: window.location.href
                },
                success: function (result) {
                    //alert(result.appId)
                    var appID = result.appId
                    var redirectUri = encodeURI(urls); // 授权成功后重定向地址
                    var state = rukouName; // 状态标识（用于项目拓展)
                    // 跳转授权(拿到用户信息)
                    var strUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
                        appID +
                        "&redirect_uri=" + redirectUri +
                        "&response_type=code&scope=snsapi_base&state=" + state +
                        "#wechat_redirect";
                    window.location.href = strUrl;
                }
            })
        }
        // 小区
        $.post(ymt_api+"/ymtUser/query", {
            wxcode: code,
            name: rukouName
        }, function (res) {
            //alert(res.code)
            // 此时返回了openid
            if (res.code == 1) {
                //alert("未注册过")
                getCode("http://hesuan.city1000.cn/code/registerFirst.html")
            } else if (res.code == 0) {
                //alert("注册了")
                if(res.data.status==0){
                    // 跳两个按钮的页面
                    // getCode("http://hesuan.city1000.cn/code/YesOrNo.html")
                    // openid和rukouName(此时需要把openid带过去)
                    var open_data=`${rukouName}|${res.data.openId}`
                    window.location.href="http://hesuan.city1000.cn/code/community/YesOrNo.html?state="+open_data
                }else if(res.data.status==1){
                    // 本单位(此时open_data里边有入口地址和openid和单位密码)
                    var open_data=`${rukouName}|${res.data.openId}`
                    window.location.href="http://hesuan.city1000.cn/code/community/yes.html?state="+open_data
                }else if(res.data.status==2){
                    // 来访人员(此时open_data里边有入口地址和openid和单位名称)
                    var open_data=`${rukouName}|${res.data.openId}|${res.data.bank}`
                    window.location.href="http://hesuan.city1000.cn/code/community/no.html?state="+open_data
                }
            }
        })
    </script>
</body>

</html>